package org.vy.dal.dao;

import java.util.List;

import org.vy.dal.QueryManager;

public class VySatsangDAO {
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE SATSANG_ID = m_SatsangId";
	private static final String FIND_BY_PARAMETER = "SELECT * FROM <TABLE> WHERE 1 = 1 ";
	private static final String UPDATE = "UPDATE <TABLE> SET REF_VY_ID = m_RefId,SATSANG_ADDRESS_ID=m_SatsangAddress,PRIMARY_CONTACT_VY_ID=m_PrimaryContactVyId,SATSANG_DAYS=m_SatsangDay,START_TIME=m_SatsangTimestart,END_TIME=m_SatsangTimeend,REGULAR_OR_ONCE=m_RegularityOrOnce WHERE SATSANG_ID = m_SatsangId";
	private static final String INSERT = "INSERT INTO <TABLE> (SATSANG_ID, REF_VY_ID, SATSANG_ADDRESS_ID, PRIMARY_CONTACT_VY_ID, SATSANG_DAYS, START_TIME, END_TIME, REGULAR_OR_ONCE) VALUES(m_SatsangId,m_RefId,m_SatsangAddress,m_PrimaryContactVyId,m_SatsangDay,m_SatsangTimestart,m_SatsangTimeend,m_RegularityOrOnce)";
	private static final String DELETE = "DELETE FROM <TABLE> WHERE SATSANG_ID = m_SatsangId";

	public static VySatsangDO findByPK(String key) throws Exception {
		VySatsangDO dto = new VySatsangDO();
		dto.setSatsangId(key);
		dto = (VySatsangDO) QueryManager.executeQuery(dto, FIND_BY_PK);
		return dto;
	}
	public static VySatsangDO insert(VySatsangDO dto) throws Exception {

		dto = (VySatsangDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}
	
	public static VySatsangDO update(VySatsangDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if(i > 0)
			return dto;
		else
			return null;
	}
	public static VySatsangDO delete(String key) throws Exception {
		VySatsangDO dto = new VySatsangDO();
		dto.setSatsangId(key);
		boolean i = QueryManager.executeDelete(dto, DELETE);
		if (i == true)
			return dto;
		else
			return null;
	}

	@SuppressWarnings("unchecked")
	public static List<VySatsangDO> findByParam(VySatsangDO dto)
			throws Exception {

		StringBuilder sb = new StringBuilder(FIND_BY_PARAMETER);
		if (dto.getSatsangId() != null) {
			sb.append(" AND SATSANG_ID LIKE '%" + dto.getSatsangId() + "%' ");
		}
		if (dto.getRefId() != null) {
			sb.append(" AND REF_VY_ID LIKE '%" + dto.getRefId()
					+ "%' ");
		}
		if (dto.getSatsangAddress() != null) {
			sb.append(" AND SATSANG_ADDRESS_ID LIKE '%" + dto.getSatsangAddress() + "%' ");

		}
		if (dto.getPrimaryContactVyId() != null) {
			sb.append(" AND PRIMARY_CONTACT_VY_ID LIKE '%" + dto.getPrimaryContactVyId() + "%' ");
		}
		if (dto.getSatsangDay() != null) {
			sb.append(" AND SATSANG_DAYS LIKE '%" + dto.getSatsangDay() + "%' ");
		}
		if (dto.getSatsangTimestart() != null) {
			sb.append(" AND START_TIME LIKE '%" + dto.getSatsangTimestart() + "%' ");

		}
		if (dto.getSatsangTimeend() != null) {
			sb.append(" AND END_TIME LIKE '%" + dto.getSatsangTimeend() + "%' ");

		}
		if (dto.getRegularityOrOnce() != null) {
			sb.append(" AND REGULAR_OR_ONCE LIKE '%" + dto.getRegularityOrOnce() + "%' ");

		}
	
		System.out.println(sb.toString());
		return (List<VySatsangDO>) QueryManager.executeListQuery(dto, sb
				.toString());
	}

}
